#!/bin/bash
#############################################
# author:fang
# version : v1.0
# name  : xd
# dispcripe: uses of log daily
# CopyRight@fangyunjiang[42550564@qq.com]
#############################################

source ~/command/common
file=~/.daily
his=~/.daily_history

if [ "$1" = "-h" -o "$1"  = "--help" ];then
    echo ""
    echo "xd Usage:"
    echo ""
    echo "  xd"
    echo "  xd git [n]: set from clipboard from git lg, n for from log"
    echo "  xd line: set line from clipboard"
    echo "  xd clear"
    echo "  xd history: show history"
    echo "  xd copy"
    echo "  xd vim/code/reverse"
    echo "  xd n[i|a|r|e|e][text]: 插入|追加|替换|删除|执行 第n行"
    echo "     xd 3 -> 拷贝第3行"
    echo "     xd 3i text -> 第3行前面插入text"
    echo "     xd 3a text -> 第3行后面追加text"
    echo "     xd 3r text -> 替换第3行为text"
    echo "     xd 3d -> 删除第3行"
    echo "     xd 3e -> 执行第3行"
    echo ""
    exit
fi

if [ "$1" = "" ];then
    cat ${file} 2>/dev/null|sed '/^\s*$/d'|sed =|sed 'N;s/\n/: /'
    exit
fi

if [ "$1" = "git" ];then
    if [ "$2" -gt 0 ] 2>/dev/null;then
        git lg -$2 |sed 's#.*]:##'|sed '/^\s*$/d'|sed '1!G;h;$!d' >> ${file}
        echo >> ${file}
        exit
    else
        if __mac ;then
            pbpaste|sed 's#.*]:##'|sed '/^\s*$/d'|sed '1!G;h;$!d' >> ${file}
        else
            cat /dev/clipboard|sed 's#.*]:##'|sed '/^\s*$/d'|sed '1!G;h;$!d' >> ${file}
        fi
        echo >> ${file}
        exit
    fi
fi

if [ "$1" = "line" ];then
    if __mac ;then
        pbpaste|sed '/^\s*$/d' >> ${file}
    else
        cat /dev/clipboard|sed '/^\s*$/d' >> ${file}
    fi
    echo >> ${file}
    exit
fi

if [ "$1" = "copy" ];then
    echo "=================== start copy below:"
    cat ${file} 2>/dev/null|sed '/^\s*$/d'|sed =|sed 'N;s/\n/: /'
    if __mac ;then
        cat ${file} 2>/dev/null|sed '/^\s*$/d'|sed =|sed 'N;s/\n/: /'|pbcopy
    else
        cat ${file} 2>/dev/null|sed '/^\s*$/d'|sed =|sed 'N;s/\n/: /' > /dev/clipboard
    fi
    echo "=================== success copy above"
    exit
fi

if [ "$1" = "history" ];then
    cat ${his} 2>/dev/null
    exit
fi

if [ "$1" = "clear" ];then
    cat ${file} >> ${his}
    echo -n > ${file}
    echo "clear success"
    exit
fi

if [ "$1" = "code" ];then
    code ${file}
    exit
fi

if [ "$1" = "vim" ];then
    vim ${file}
    exit
fi

if [ "$1" = "reverse" ];then
    sed -i '1!G;h;$!d' ${file}
    exit
fi

if [[ "$1" =~ ^[0-9]{1,2}$ ]];then # 拷贝某一行
    if __mac ;then
        cat ${file} 2>/dev/null|sed -n ''${1}'p'|pbcopy
    else
        cat ${file} 2>/dev/null|sed -n ''${1}'p' > /dev/clipboard
    fi
    echo "第${1}行已复制到剪切板"
    exit
elif [[ "$1" =~ ^[0-9]{1,2}d$ ]];then # 删除某一行
    n=${1::-1}
    sed -i ''$n'd' ${file} #删除第n行
    echo "第${n}行已删除"
    exit
elif [[ "$1" =~ ^[0-9]{1,2}e$ ]];then # 执行某一行
    n=${1::-1}
    echo "第${n}行已执行"
    cmd=`cat ${file} 2>/dev/null|sed -n ''${n}'p'`
    echo $cmd
    $cmd
    exit
elif [[ "$1" =~ ^[0-9]{1,2}a$ ]];then # 某一行后追加
    n=${1::-1}
    shift
    text="$@"
    sed -i ''$n'a\'$text'' ${file} #在第n行后插入一行
    exit
elif [[ "$1" =~ ^[0-9]{1,2}i$ ]];then # 某一行前插入
    n=${1::-1}
    shift
    text="$@"
    sed -i ''$n'i\'$text'' ${file} #在第n行前插入一行
    exit
elif [[ "$1" =~ ^[0-9]{1,2}r$ ]];then # 替换某一行
    n=${1::-1}
    shift
    text="$@"
    sed -i ''$n'a\'$text'' ${file} #在第n行前插入一行
    sed -i ''$n'd' ${file} #删除第n行
    exit
fi

echo "$@" >> ${file}
